<template>
  <div>
    <van-nav-bar title="登录" />
    <div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="user.mobile"
          required
          name="mobile"
          label="手机号"
          placeholder="请输入11位手机号"
          :rules="[
            {
              required: true,
              message: '请填写11位有效手机号',
              pattern: /^1[3-9]\d{9}$/,
            },
          ]"
        />
        <van-field
          v-model="user.code"
          required
          type="password"
          name="code"
          label="密码"
          placeholder="请输入6位的密码"
          :rules="[
            { required: true, message: '请填写密码', pattern: /^\d{6}$/ },
          ]"
        />
        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            :loading="isLoding"
            :disabled="isLoding"
            loading-text="程序员拼命加载ing..."
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { MyNotify } from '@/utils/Notify.js'
import { setToken } from '@/utils/token'
import { setStorage } from '@/utils/storage'
export default {
  data () {
    return {
      user: {
        mobile: '13888888889', // 手机号
        code: '246810' // 验证码(密码-必须是246810后台规定s的，无论手机号是什么)
      },
      isLoding: false // 加载状态
    }
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      this.isLoding = true
      try {
        const res = await loginAPI(this.user)
        console.log(res)
        MyNotify({ type: 'success', message: '登录成功' })
        setToken(res.data.data.token)
        setStorage('refresh_token', res.data.data.refresh_token)
        this.isLoding = false
        this.$router.replace({
          path: this.$route.query.path || '/layout/home'
        })
      } catch (error) {
        MyNotify({ type: 'warning', message: '账号或密码错误' })
        this.isLoding = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
// .van-nav-bar {
//   background: #007bff;
// }

// /deep/ .van-nav-bar__title {
//   color: white;
// }
</style>
